#ifndef SEARCH_H_INCLUDED
#define SEARCH_H_INCLUDED
#include <time.h>

string search_tree(PNODE tree,const char *title);//搜索标签
void erase(string &str,const string &er);

void erase(string &str,const string &er){
    size_t pos;
    while((pos=str.find(er))!=string::npos){
        str.erase(pos,er.length());
    }
}


string search_tree(PNODE tree,const char *title) {
#if DEBUG
    long now=clock();
    cout<<now<<'-';
#endif
    string stitle=title;
    string result;//结果
    PNODE node=tree->son;
    string varcode;
    int L=1;
    while (L) {
        switch (L) {
            case 1://处理一个节点
                if (node->feature.size()==0) {
                    if (node->son!=NULL) { //没有feature，且有子节点时直接进入下一层
                        L=5;
                        break;
                        }
                    else { //没有feature且没有子节点，即叶子节点，开始搜索标签
                        for ( vector<CONTENT>::iterator it = node->value.begin(); it != node->value.end(); ++it ) {
                            if (stitle.find((*it).nickname)!=string::npos) {
                                erase(stitle,(*it).nickname);//删除匹配到的字符串
                                (*it).num+=1;//该标签下值增1
                                node->num_total+=1;//节点值加1
                                result+=node->varcode+"^"+(*it).name+"&&";
                                PNODE tmp=node;
                                while(tmp->father!=tree){
                                    tmp=tmp->father;
                                    tmp->num+=1;
                                }
                                    tree->num+=1;
                                    //if(varcode.empty()) varcode=tmp->varcode;
                                L=4;
                                break;
                                }
                            }
                        }
                    }
                else {
                    L=4;
                    int i=0;
                    for ( vector<CONTENT>::iterator it = node->feature.begin(); it != node->feature.end(); ++it,++i ) {
                        if (strstr(title,(*it).nickname.c_str())!=0) {
                            (*it).num+=1;//该特征下值增1
                            varcode=node->varcode;
                            L=5;
                            break;
                            }
                        }
                    break;
                    }
            case 4://实现去往下一个节点
                if ((node->father->num==0 || node->father->tag_continue==1) && node->brother!=NULL) {
                    node->num_total+=node->num;
                    node->num=0;
                    node=node->brother;
                    L=1;
                    break;
                    }
                else if(node->father!=NULL) {
                    node->num_total+=node->num;
                    node->num=0;
                    node=node->father;
                    L=4;
                    break;
                    }
                else {
                    L=0;
                    break;
                    }
            case 5://深入一个节点
                if (node->son!=NULL) {
                    node=node->son;
                    L=1;
                    break;
                    }
                else {
                    L=4;
                    break;
                    }
            }
        if (node->father==NULL) break;//遍历结束
        }
    tree->num=0;
#if DEBUG
    long now2=clock();
    cout<<now2<<'='<<(now2-now)*1.0/CLOCKS_PER_SEC<<endl;
#endif
    return result;
    }
#endif // SEARCH_H_INCLUDED
